package com.atguigu.edu.publisher.controller;

import com.atguigu.edu.publisher.beans.VideoPlaySecBean;
import com.atguigu.edu.publisher.beans.VideoViewerNumsBean;
import com.atguigu.edu.publisher.service.VideoPlaySecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.atguigu.edu.publisher.utils.DateFormatUtil.now;

/**
 * @Classname VideoPlaySecController
 * @Date 2022/11/20 15:19
 * @Created by arun
 */
@RestController
public class VideoPlaySecController {

    @Autowired
    private VideoPlaySecService videoPlaySecService;

    @RequestMapping("/play/videoPlaySec")
    public String selectVideoPlaySec(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = now();
        }

        List<VideoPlaySecBean> videoPlaySecBeans = videoPlaySecService.selectVideoPlaySec(date);

        StringBuilder json = new StringBuilder("{\"status\": 0,\"data\": { \"categories\": [");
        for (int i = 0; i < videoPlaySecBeans.size(); i++) {
            VideoPlaySecBean playSecBean = videoPlaySecBeans.get(i);
            json.append("\"" + playSecBean.getChapterId() + "\"");
            if (i < videoPlaySecBeans.size() - 1) {
                json.append(",");
            }
        }
        json.append("],\"series\":[{ \"name\" : \"视频播放时长\" ,\"data\": [");
        for (int i = 0; i < videoPlaySecBeans.size(); i++) {
            VideoPlaySecBean playSecBean = videoPlaySecBeans.get(i);
            json.append("\"" + playSecBean.getPlaySec() + "\"");
            if (i < videoPlaySecBeans.size() - 1) {
                json.append(",");
            }
        }
        json.append("]}]}}");

        return json.toString();
    }

    @RequestMapping("/play/videoViewerNums")
    public String selectVideoViewerNums(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0) {
            date = now();
        }

        List<VideoViewerNumsBean> videoViewerNumsBeans = videoPlaySecService.selectVideoViewerNums(date);

        StringBuilder json = new StringBuilder("{\"status\": 0,\"data\": { \"categories\": [");
        for (int i = 0; i < videoViewerNumsBeans.size(); i++) {
            VideoViewerNumsBean viewerNumsBean = videoViewerNumsBeans.get(i);
            json.append("\"" + viewerNumsBean.getVideoId() + "\"");
            if (i < videoViewerNumsBeans.size() - 1) {
                json.append(",");
            }
        }
        json.append("],\"series\":[{ \"name\" : \"视频观看人次\" ,\"data\": [");
        for (int i = 0; i < videoViewerNumsBeans.size(); i++) {
            VideoViewerNumsBean viewerNumsBean = videoViewerNumsBeans.get(i);
            json.append("\"" + viewerNumsBean.getNums() + "\"");
            if (i < videoViewerNumsBeans.size() - 1) {
                json.append(",");
            }
        }
        json.append("]}]}}");

        return json.toString();
    }
}
